<?php

/**
 * @Desc:用户开户接口
 * @author dasheng@baofu.com(大圣)
 * @datetime 2025/4/6 15:15
 */

require_once "../../Init/init.php";

use App\baocaitong3\Entity\AccInfoEntity as AccInfo;
use App\baocaitong3\Entity\ContentBodyEntity as ContentBody;
use App\baocaitong3\Entity\HeaderEntity as Header;
use App\baocaitong3\Entity\OpenAccountEntity as OpenAccount;
use App\baocaitong3\Entity\PostParmEntity as PostParm;
use App\baocaitong3\SecurityUtil\NewRsaUtil;
use App\baocaitong3\Util\HttpClient;
use App\baocaitong3\Util\Log;
use App\baocaitong3\Util\Tools;

$ServiceTp = "BCT3-1002-001-01";
$accType = 2;
// 企业开户
$Email = "greatwalltesta@baofu.com";//企业开户邮箱
$SelfEmployed = false;//是否个体户 企业为false，不传默认为false
$CorporateName ="杨雪宇";//法人姓名
$CorporateCertType = "ID";//法人证件类型：身份证-ID，港澳通行证-HONG_KONG_AND_MACAO_PASS、台湾同胞来往内地通行证-TAIWAN_TRAVEL_PERMIT、护照-PASSPORT
$CorporateCertId = "542624198102098035";//法人证件号码
$CorporateMobile = "18756161713";//法人手机号
$IndustryId="9999";
$BankName="中国工商银行";//银行名称
$DepositBankProvince ="江苏省";//开户行省份
$DepositBankCity = "南京市";//开户行城市
$DepositBankName = "支行营业部";//开户支行名称
$RegisterCapital = "50000";//注册资本
// 个人开户
$MobileNo="17127004795";//手机号

// 公共
$CardNo="7881620854713";//卡号 个人为对私卡号，企业为对公卡号
$CustomerName ="琴海文科技股份有限公司";//客户名称,当为企业/个体时为企业名称
$CertificateNo ="91510725913854383A";//证件号，当为个人时上送身份证号，企业时上送营业执照号

$bodyinfo = new OpenAccount();
$bodyinfo->accType = $accType;
$bodyinfo->version = "1.0.0";
$bodyinfo->businessType = "BCT3";
$bodyinfo->noticeUrl = "http://10.1.71.81:9086/Recive/open_account_notify.php";
$accinfo = new AccInfo();
$accinfo->transSerialNo = Tools::getTsnOderid("TSN");
$accinfo->loginNo = Tools::getTsnOderid("LN");
$accinfo->customerName = $CustomerName;
$accinfo->certificateNo = $CertificateNo;
$accinfo->cardNo = $CardNo;
$accinfo->needUploadFile = false;
Log::EchoFormat("流水号：".$accinfo->transSerialNo);
Log::EchoFormat("登录号：".$accinfo->loginNo);
if($accType == 1)
{
    $accinfo->certificateType = "ID";
    $accinfo->mobileNo = $MobileNo;
    $accinfo->cardUserName = $CustomerName;
}else if($accType == 2){
    $accinfo->certificateType = "LICENSE";
    $accinfo->email = $Email;
    $accinfo->selfEmployed = $SelfEmployed;
    $accinfo->corporateName = $CorporateName;
    $accinfo->corporateCertType = $CorporateCertType;
    $accinfo->corporateCertId = $CorporateCertId;
    if($SelfEmployed) $accinfo->corporateMobile = $CorporateMobile;
    $accinfo->industryId = $IndustryId;
    $accinfo->bankName = $BankName;
    $accinfo->depositBankProvince = $DepositBankProvince;
    $accinfo->depositBankCity = $DepositBankCity;
    $accinfo->depositBankName = $DepositBankName;
    $accinfo->registerCapital = $RegisterCapital;
}else throw new Exception("账户类型不存在");

if($accinfo->needUploadFile) $accinfo->qualificationTransSerialNo = "";////[必传]资质文件流水,是否上传请咨询业务对接人
$bodyinfo->accInfo = $accinfo;

$headerinfo = new Header();
$headerinfo->memberId = $GLOBALS["MemberId"];
$headerinfo->terminalId = $GLOBALS["TerminalId"];
$headerinfo->serviceTp = $ServiceTp;
$headerinfo->verifyType = "1";

$contBody = new ContentBody();
$contBody->header = $headerinfo;
$contBody->body = $bodyinfo;

$ContentStr = json_encode($contBody);
Log::EchoFormat("JSON序列号：".$ContentStr);

$enContent = NewRsaUtil::encryptByPfx($ContentStr,$GLOBALS["PFX_PATH"],$GLOBALS["KeyPwd"]);

$postParm = new PostParm();
$postParm->memberId = $GLOBALS["MemberId"];
$postParm->terminalId = $GLOBALS["TerminalId"];
$postParm->content = $enContent;

$ResultPost  = HttpClient::Post($postParm,"https://vgw.baofoo.com/union-gw/api/".$ServiceTp."/transReq.do");
Log::EchoFormat("返回参数：".$ResultPost);
if(empty($ResultPost)) throw new Exception("请求返回异常");
if(strpos($ResultPost,"header") != false)
{
    $resulterror = json_decode($ResultPost,true);
    var_dump($resulterror);
}else{
    $DePostString = NewRsaUtil::decrptByPub($ResultPost,$GLOBALS["CER_PATH"]);
    Log::EchoFormat("解密返回：".$DePostString);
    if(empty($DePostString))  throw new Exception("解密空值，请检查证书是否正常");
    if(strpos($DePostString,"header"))
    {
        $contenBe = json_decode($DePostString,true);
        if($contenBe["header"]["sysRespCode"] === "S_0000")
        {
            Log::EchoFormat("业务层结果:".json_encode($contenBe["header"]));
            if($contenBe["body"]["retCode"] === 1){
                /**
                 * 以 state 状态为准
                 */
                Log::EchoFormat("结果:".json_encode($contenBe["body"]));
            }
        }
    }
}
